package com.gxa.autoparts.portal.service.impl;

import com.gxa.autoparts.core.pojo.Comment;
import com.gxa.autoparts.core.pojo.GoodsImg;
import com.gxa.autoparts.core.pojo.KeyWords;
import com.gxa.autoparts.core.util.JsonUtil;
import com.gxa.autoparts.core.util.StringUtil;
import com.gxa.autoparts.portal.mapper.GoodsImgMapper;
import com.gxa.autoparts.portal.mapper.KeyWordsMapper;
import com.gxa.autoparts.portal.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：looker
 * @date ：Created in 2022/1/10 10:51
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public List<Comment> getComments(Long goodId) {
        List<Comment> comments = new ArrayList<>();

        ListOperations<String, Object> listOperations = redisTemplate.opsForList();
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        List<Object> range = listOperations.range("COMMENTS:" + goodId, 0, -1);
        for (Object o : range) {
            String s = valueOperations.get("COMMENT:" + o.toString()).toString();
            Boolean empty = StringUtil.isEmpty(s);
            if (!empty) {
                Comment comment = JsonUtil.readToObject(s, Comment.class);
                comments.add(comment);
            }
        }
        return comments;
    }

    @Autowired
    KeyWordsMapper keyWordsMapper;

    @Override
    public List<Object> getKeyWords(Long goodId) {
        List<Object> words = new ArrayList<>();
        String key = "GOODKW:" + goodId;
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();
        Boolean aBoolean = redisTemplate.hasKey(key);
        if(!aBoolean){
            Example example = new Example(KeyWords.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("goodsId",goodId);
            List<KeyWords> keyWords = keyWordsMapper.selectByExample(example);
            for (KeyWords keyWord : keyWords) {
                words.add(keyWord.getName());
            }
            if(words.size()>0){
                listOperations.leftPushAll(key,words);
            }
            return words;
        }
        List<Object> range = listOperations.range(key, 0, -1);

        return range;
    }

    @Autowired
    GoodsImgMapper goodsImgMapper;

    @Override
    public List<Object> getImgs(Long goodId) {
        ListOperations<String, Object> listOperations = redisTemplate.opsForList();
        List<Object> imgs = new ArrayList<>();
        String key = "IMG:"+goodId;
        Boolean aBoolean = redisTemplate.hasKey(key);
        if (!aBoolean) {
            Example example = new Example(GoodsImg.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("goodsId",goodId);
            List<GoodsImg> goodsImgs = goodsImgMapper.selectByExample(example);
            for (GoodsImg goodsImg : goodsImgs) {
                imgs.add(goodsImg.getImg());
            }
            //防止为空
            if(imgs.size()==0){
                imgs.add("");
            }
            listOperations.leftPushAll(key,imgs);
            return imgs;
        }
        List<Object> range = listOperations.range(key, 0, -1);
        return range;
    }

    @Override
    public Boolean isResovle(Long goodId) {
        return null;
    }
}
